package com.itanty.spring.Impl;

import com.itanty.spring.dao.UserDao;
import com.itanty.spring.entity.User;
import com.itanty.spring.rowmapper.UserRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * @date 2025/9/24
 */
public class UserDaoImpl implements UserDao {
    private JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public void insertUser(User user) {
        String sql = new StringBuffer()
                .append(" insert into ")
                .append(" t_user (username,password,phone,address) ")
                .append(" value (?,?,?,?) ")
                .toString();
        jdbcTemplate.update(sql,user.getUsername(),user.getPassword(),user.getPhone(),user.getAddress());
    }

    @Override
    public void deleteById(Integer id) {
        String sql = new StringBuffer()
                .append(" delete from ")
                .append(" t_user ")
                .append(" where id = ? ")
                .toString();
        jdbcTemplate.update(sql,id);
    }

    @Override
    public void updateUser(User user) {
        String sql = new StringBuffer()
                .append(" update t_user ")
                .append(" set ")
                .append(" username = ? , ")
                .append(" password = ? , ")
                .append(" phone = ? , ")
                .append(" address = ?  ")
                .append(" where id = ? ")
                .toString();
        jdbcTemplate.update(sql,user.getUsername(),user.getPassword(),user.getPhone(),user.getAddress(),user.getId());
    }

    @Override
    public User selectById(Integer id) {
        String sql = new StringBuffer()
                .append(" select * ")
                .append(" from t_user ")
                .append(" where id = ? ")
                .toString();
        User user = jdbcTemplate.queryForObject(sql,new UserRowMapper(),id);
        return user;
    }

    @Override
    public List<User> selectAll() {
        String sql = new StringBuffer()
                .append(" select id,username,password,phone,address  ")
                .append(" from t_user ")
                .toString();
        List<User> users = jdbcTemplate.query(sql,new UserRowMapper());
        return users;
    }

    @Override
    public Integer insertReturnPrimaryKey(User user) {
        KeyHolder keyHolder = new GeneratedKeyHolder();
        jdbcTemplate.update(new PreparedStatementCreator(){
            @Override
            public PreparedStatement createPreparedStatement(Connection conn)throws SQLException {
                String sql = new StringBuffer()
                        .append(" insert into ")
                        .append(" t_user (username , password , phone , address) ")
                        .append(" value (?,?,?,?) ")
                        .toString();
                PreparedStatement ps = conn.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS);
                ps.setString(1,user.getUsername());
                ps.setString(2,user.getPassword());
                ps.setString(3,user.getPhone());
                ps.setString(4,user.getAddress());
                return ps;
            }
        },keyHolder);
        return keyHolder.getKey().intValue();
    }
}
